package com.whfc.iot.mach.codec;

import com.whfc.iot.mach.entity.MachMsg;
import com.whfc.iot.mach.util.MachMsgUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 冷藏车硬件消息-编码器
 */
public class MachMsgEncoder extends MessageToMessageEncoder<MachMsg> {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    protected void encode(ChannelHandlerContext ctx, MachMsg msg, List<Object> out) throws Exception {

        ByteBuf buf = Unpooled.buffer(msg.getTotalLength());

        MachMsgUtil.encode(buf, msg);

        //发送
        logger.debug("发送数据:{}", ByteBufUtil.prettyHexDump(buf));
        out.add(buf);
    }
}
